
package backupUtility;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileListUI  extends JFrame implements ActionListener{
    private JPanel scrollPanel;
    private JPanel buttonPanel;
    private JScrollPane scrollPane;
    private JList choicesList;
    private FileListGetter getList;
    private JButton saveButton;
    private JButton cancelButton;
    private JLabel chooseLabel;
    private ArrayList<File> arrayListofFiles;
    
    public FileListUI(){
        getList = new FileListGetter();
        arrayListofFiles = getList.getArrayListofFiles();
        Collections.sort(arrayListofFiles);
        Object [] fileNames = new Object[arrayListofFiles.size()];
        for(int i = 0; i < fileNames.length; i++){
            fileNames[i] = arrayListofFiles.get(i).getName();
        }
       
        choicesList = new JList(fileNames);
        choicesList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        choicesList.setLayoutOrientation(JList.HORIZONTAL_WRAP);
        choicesList.setVisibleRowCount(-1);
        
        scrollPane = new JScrollPane(choicesList);
        scrollPane.setPreferredSize(new Dimension(700,400));
        
        saveButton = new JButton("Backup Files!");
        saveButton.addActionListener(this);
        
        cancelButton = new JButton("Cancel");
        cancelButton.addActionListener(this);
        
        chooseLabel = new JLabel("Select Files to Backup: ");
      
        scrollPanel = new JPanel();
        scrollPanel.setLayout(new BoxLayout(scrollPanel, BoxLayout.PAGE_AXIS));
        scrollPanel.add(chooseLabel);
        scrollPanel.add(Box.createRigidArea(new Dimension(0,5)));
        scrollPanel.add(scrollPane);
        scrollPanel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));

        buttonPanel = new JPanel();
        buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
        buttonPanel.setBorder(BorderFactory.createEmptyBorder(0, 10, 10, 10));
        buttonPanel.add(Box.createHorizontalGlue());
        buttonPanel.add(saveButton);
        buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));
        buttonPanel.add(cancelButton);
        
        Container contentPane = getContentPane();
        contentPane.add(scrollPanel, BorderLayout.CENTER);
        contentPane.add(buttonPanel, BorderLayout.PAGE_END);

        pack();
        setVisible(true);
        setSize(800,500);
        setResizable(false);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    
    public void actionPerformed(ActionEvent ae){
        if(ae.getSource()==saveButton){
        int [] selectedIndexes = choicesList.getSelectedIndices();
        ArrayList<File> selectedFiles = new ArrayList<File>();
        for(int i = 0; i < selectedIndexes.length; i++){
            System.out.println(arrayListofFiles.get(selectedIndexes[i]).getName());
            selectedFiles.add(arrayListofFiles.get(selectedIndexes[i]));
        }   
            try {
                BackupLogic backup = new BackupLogic(selectedFiles);
            } catch (IOException ex) {
                Logger.getLogger(FileListUI.class.getName()).log(Level.SEVERE, null, ex);
            }
        }else if(ae.getSource()==cancelButton){
            BackupUtilityUI reset = new BackupUtilityUI();
            String[] args = new String[0];
            reset.main(args);
            dispose();
        }
    
    }
}
